--- |
 ; pr37472
 ; These test verify that shrink-wrap does not set the restore point
 ; to a position where the stack might still be accessed by a load or store
 
 ; RUN: llc -x=mir -simplify-mir -run-pass=shrink-wrap -o - %s | FileCheck %s
 ; CHECK:      name:            compiler_pop_stack
 ; CHECK:      frameInfo:       
 ; CHECK-NOT:  savePoint:
 ; CHECK-NOT:  restorePoint:
 ; CHECK:      stack:
 ; CHECK:      name:            f
 ; CHECK:      frameInfo:       
 ; CHECK:      savePoint:       '%bb.2'
 ; CHECK-NEXT: restorePoint:    '%bb.4'
 ; CHECK-NEXT: stack:

  target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
  target triple = "aarch64-arm-none-eabi"

  %struct.S = type { i32, i32 }

  @__const.f.arr = private unnamed_addr constant [4 x i8] c"\01\02\03\04", align 1


  declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture)


  declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture)


  declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture writeonly, i8* nocapture readonly, i64, i1 immarg)
 ; Test from: https://bugs.llvm.org/show_bug.cgi?id=42136
  define void @compiler_pop_stack(i32 %num) {
  entry:
    %rstack = alloca [16 x i32], align 4
    %tmp = bitcast [16 x i32]* %rstack to i8*
    call void @llvm.lifetime.start.p0i8(i64 64, i8* nonnull %tmp)
    %cmp = icmp ult i32 %num, 2
    br i1 %cmp, label %cleanup, label %if.end

  if.end:
    %arrayidx1 = bitcast [16 x i32]* %rstack to i32*
    store volatile i32 %num, i32* %arrayidx1, align 4
    br label %while.body

  while.body:
    %ptr.017 = phi i32 [ 1, %if.end ], [ %ptr.1, %if.end7 ]
    %dec = add i32 %ptr.017, -1
    %idxprom = zext i32 %dec to i64
    %arrayidx2 = getelementptr inbounds [16 x i32], [16 x i32]* %rstack, i64 0, i64 %idxprom
    %tmp1 = load volatile i32, i32* %arrayidx2, align 4
    %cmp3 = icmp eq i32 %tmp1, 0
    br i1 %cmp3, label %if.end7, label %if.then4

  if.then4:
    %sunkaddr = mul i64 %idxprom, 4
    %0 = bitcast [16 x i32]* %rstack to i8*
    %sunkaddr2 = getelementptr inbounds i8, i8* %0, i64 %sunkaddr
    %1 = bitcast i8* %sunkaddr2 to i32*
    store volatile i32 %tmp1, i32* %1, align 4
    br label %if.end7

  if.end7:
    %ptr.1 = phi i32 [ %ptr.017, %if.then4 ], [ %dec, %while.body ]
    %cmp1 = icmp eq i32 %ptr.1, 0
    br i1 %cmp1, label %cleanup, label %while.body

  cleanup:
    %2 = bitcast [16 x i32]* %rstack to i8*
    call void @llvm.lifetime.end.p0i8(i64 64, i8* nonnull %2)
    ret void
  }
 ; Test from: https://bugs.llvm.org/show_bug.cgi?id=37472
  define i32 @f(%struct.S* nocapture %arg, i32 %arg1) {
  bb:
    %tmp = alloca [4 x i8], align 1
    %tmp2 = icmp ugt i32 %arg1, 4
    br i1 %tmp2, label %bb16, label %bb3

  bb3:
    %tmp41 = bitcast [4 x i8]* %tmp to i8*
    call void @llvm.lifetime.start.p0i8(i64 4, i8* nonnull %tmp41)
    call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull align 1 %tmp41, i8* align 1 getelementptr inbounds ([4 x i8], [4 x i8]* @__const.f.arr, i64 0, i64 0), i64 4, i1 true)
    %tmp5 = zext i32 %arg1 to i64
    %tmp6 = getelementptr inbounds [4 x i8], [4 x i8]* %tmp, i64 0, i64 %tmp5
    %tmp7 = load volatile i8, i8* %tmp6, align 1
    %tmp8 = zext i8 %tmp7 to i32
    %tmp92 = bitcast %struct.S* %arg to i32*
    store i32 %tmp8, i32* %tmp92, align 4
    %tmp10 = icmp ult i32 %arg1, 3
    br i1 %tmp10, label %bb11, label %bb15

  bb11:
    %0 = bitcast [4 x i8]* %tmp to i8*
    %sunkaddr = getelementptr inbounds i8, i8* %0, i64 %tmp5
    %tmp12 = load volatile i8, i8* %sunkaddr, align 1
    %tmp13 = zext i8 %tmp12 to i32
    %tmp14 = getelementptr inbounds %struct.S, %struct.S* %arg, i64 0, i32 1
    store i32 %tmp13, i32* %tmp14, align 4
    br label %bb15

  bb15:
    %1 = bitcast [4 x i8]* %tmp to i8*
    call void @llvm.lifetime.end.p0i8(i64 4, i8* nonnull %1)
    br label %bb16

  bb16:
    %tmp17 = phi i32 [ 0, %bb15 ], [ 1, %bb ]
    ret i32 %tmp17
  }


  declare void @llvm.stackprotector(i8*, i8**)

...
---
name:            compiler_pop_stack
alignment:       4
tracksRegLiveness: true
liveins:
  - { reg: '$w0' }
frameInfo:
  maxAlignment:    4
  maxCallFrameSize: 0
  localFrameSize:  64
stack:
  - { id: 0, name: rstack, size: 64, alignment: 4, stack-id: default, local-offset: -64 }
machineFunctionInfo: {}
body:             |
  bb.0.entry:
    liveins: $w0

    dead $wzr = SUBSWri renamable $w0, 2, 0, implicit-def $nzcv
    Bcc 3, %bb.6, implicit killed $nzcv
    B %bb.1

  bb.1.if.end:
    liveins: $w0

    STRWui killed renamable $w0, %stack.0.rstack, 0 :: (volatile store (s32) into %ir.arrayidx1)
    renamable $w9 = MOVi32imm 1
    renamable $x8 = ADDXri %stack.0.rstack, 0, 0

  bb.2.while.body:
    successors: %bb.3(0x30000000), %bb.4(0x50000000)
    liveins: $w9, $x8

    renamable $w10 = SUBWri renamable $w9, 1, 0, implicit-def $x10
    renamable $w11 = LDRWroW renamable $x8, renamable $w10, 0, 1 :: (volatile load (s32) from %ir.arrayidx2)
    CBNZW renamable $w11, %bb.4

  bb.3:
    liveins: $x8, $x10

    renamable $w9 = COPY renamable $w10, implicit killed $x10
    B %bb.5

  bb.4.if.then4:
    liveins: $w9, $w11, $x8, $x10

    STRWroX killed renamable $w11, renamable $x8, killed renamable $x10, 0, 1 :: (volatile store (s32) into %ir.1)

  bb.5.if.end7:
    successors: %bb.6(0x04000000), %bb.2(0x7c000000)
    liveins: $w9, $x8

    CBNZW renamable $w9, %bb.2
    B %bb.6

  bb.6.cleanup:
    RET_ReallyLR

...
---
name:            f
alignment:       4
tracksRegLiveness: true
liveins:
  - { reg: '$x0' }
  - { reg: '$w1' }
frameInfo:
  maxAlignment:    4
  maxCallFrameSize: 0
  localFrameSize:  4
stack:
  - { id: 0, name: tmp, size: 4, alignment: 4, stack-id: default, local-offset: -4 }
machineFunctionInfo: {}
body:             |
  bb.0.bb:
    successors: %bb.1, %bb.2
    liveins: $w1, $x0

    dead $wzr = SUBSWri renamable $w1, 4, 0, implicit-def $nzcv
    Bcc 9, %bb.2, implicit killed $nzcv

  bb.1:
    renamable $w0 = MOVi32imm 1
    B %bb.5

  bb.2.bb3:
    successors: %bb.3, %bb.4
    liveins: $w1, $x0

    renamable $w9 = MOVi32imm 67305985
    renamable $w8 = ORRWrs $wzr, renamable $w1, 0, implicit-def $x8
    STRWui killed renamable $w9, %stack.0.tmp, 0 :: (volatile store (s32) into %ir.tmp41)
    renamable $x9 = ADDXri %stack.0.tmp, 0, 0
    renamable $w10 = LDRBBroX renamable $x9, renamable $x8, 0, 0 :: (volatile load (s8) from %ir.tmp6)
    dead $wzr = SUBSWri killed renamable $w1, 2, 0, implicit-def $nzcv
    STRWui killed renamable $w10, renamable $x0, 0 :: (store (s32) into %ir.tmp92)
    Bcc 8, %bb.4, implicit killed $nzcv
    B %bb.3

  bb.3.bb11:
    liveins: $x0, $x8, $x9

    renamable $w8 = LDRBBroX killed renamable $x9, killed renamable $x8, 0, 0 :: (volatile load (s8) from %ir.sunkaddr)
    STRWui killed renamable $w8, killed renamable $x0, 1 :: (store (s32) into %ir.tmp14)

  bb.4.bb15:
    renamable $w0 = COPY $wzr

  bb.5.bb16:
    liveins: $w0

    RET_ReallyLR implicit $w0

...
